﻿/*
 * Copyright 2008 Pimm Hogeling, Karim Sempel and Demian Sempel
 * 
 * This file is part of 8bitplays.
 * 
 * 8bitplays is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * 8bitplays is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with 8bitplays. If not, see <http://www.gnu.org/licenses/>.
 */
package nl.sandmen.team.main {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import nl.sandmen.team.audio.AlbumPlayer;
	import nl.sandmen.team.events.AlbumPlayerEvent;
	import nl.sandmen.team.events.DiscographyLoaderEvent;
	import nl.sandmen.team.events.GUIEvent;
	import nl.sandmen.team.events.PlaysManagerEvent;
	import nl.sandmen.team.gui.GUI;
	import nl.sandmen.team.gui.Reflection;
	import nl.sandmen.team.playlist.Playlist;
	/**
	 * @author Team Sandmen
	 */
	public class PlaysManager extends Sprite {
		private var albumPlayer:AlbumPlayer;
		private var dataManager:DataManager;
		private var gui:GUI;
		private var playing:Boolean;
		private var playlist:Playlist;
		private var reflection:Reflection;
		public function PlaysManager():void {
			albumPlayer = new AlbumPlayer();
			albumPlayer.addEventListener(AlbumPlayerEvent.NEXT_ALBUM, nextAlbum);
			dataManager = new DataManager();
			dataManager.addEventListener(DiscographyLoaderEvent.END_OF_LIST, endOfList);
			gui = new GUI();
			gui.setStatusText("Connecting...");
			// To reduce memory use, the graphic user interface will be built when/if it's first opened.
			addEventListener(Event.ADDED_TO_STAGE, guiInitialize);
		}
		private function endOfList(e:DiscographyLoaderEvent):void {
			dataManager.removeEventListener(DiscographyLoaderEvent.END_OF_LIST, endOfList);
			playlist = new Playlist(dataManager.unusedAlbums);
			albumPlayer.playAlbum(playlist.currentAlbum);
			if (contains(gui)) {
				gui.initializeAlbums(playlist.beforePreviousAlbum, playlist.previousAlbum, playlist.currentAlbum, playlist.nextAlbum, playlist.afterNextAlbum);
				gui.playbackButtons.setPlayState(true);
			}
			playing = true;
		}
		private function exit(e:MouseEvent):void {
			dispatchEvent(new PlaysManagerEvent(PlaysManagerEvent.EXIT));
		}
		public function greenLight():void {
			gui.setStatusText("Downloading...");
			dataManager.load();
		}
		private function guiInitialize(e:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, guiInitialize);
			if (playlist) {
				gui.initializeAlbums(playlist.beforePreviousAlbum, playlist.previousAlbum, playlist.currentAlbum, playlist.nextAlbum, playlist.afterNextAlbum);
			}
			addChild(gui);
			reflection = new Reflection(gui, 100, .4, .8);
			addChild(reflection);
			gui.bar.addEventListener(MouseEvent.DOUBLE_CLICK, minimize);
			gui.bar.addEventListener(MouseEvent.MOUSE_DOWN, startWindowDrag);
			gui.controllButtons.minimize.addEventListener(MouseEvent.CLICK, minimize);
			gui.controllButtons.exit.addEventListener(MouseEvent.CLICK, exit);
			gui.playbackButtons.playPause.addEventListener(MouseEvent.CLICK, playPause);
			gui.playbackButtons.previousAlbum.addEventListener(MouseEvent.CLICK, previousAlbum);
			gui.playbackButtons.previous.addEventListener(MouseEvent.CLICK, previous);
			gui.addEventListener(GUIEvent.GUI_PREVIOUS_ALBUM, previousAlbum);
			gui.playbackButtons.nextAlbum.addEventListener(MouseEvent.CLICK, nextAlbum);
			gui.playbackButtons.next.addEventListener(MouseEvent.CLICK, next);
			gui.addEventListener(GUIEvent.GUI_NEXT_ALBUM, nextAlbum);
			gui.playbackButtons.setPlayState(playing);
		}
		private function minimize(e:MouseEvent):void {
			dispatchEvent(new PlaysManagerEvent(PlaysManagerEvent.SHOW_HIDE));
		}
		private function next(e:MouseEvent):void {
			if (albumPlayer) {
				albumPlayer.trackDown();
			}
		}
		private function nextAlbum(e:Event):void {
			if (playlist) {
				playlist.goNextAlbum();
				albumPlayer.playAlbum(playlist.currentAlbum);
				if (contains(gui)) {
					gui.shiftAlbumLeft(playlist.afterNextAlbum);
					gui.playbackButtons.setPlayState(playing = true);
				}
			}
		}
		private function playPause(e:MouseEvent):void {
			if (albumPlayer) {
				if (playing = !playing) {
					albumPlayer.play();
				} else {
					albumPlayer.pause();
				}
				gui.playbackButtons.setPlayState(playing);
			}
		}
		private function previous(e:MouseEvent):void {
			if (albumPlayer) {
				albumPlayer.trackUp();
			}
		}
		private function previousAlbum(e:Event):void {
			if (playlist) {
				playlist.goPreviousAlbum();
				albumPlayer.playAlbum(playlist.currentAlbum);
				gui.shiftAlbumRight(playlist.beforePreviousAlbum);
				gui.playbackButtons.setPlayState(playing = true);
			}
		}
		private function startWindowDrag(e:MouseEvent):void {
			stage.nativeWindow.startMove();
		}
	}
}